<template>
  <div>
    <!-- <warning-bar
      title="获取字典且缓存方法已在前端utils/dictionary 已经封装完成 不必自己书写 使用方法查看文件内注释"
    /> -->
    <div class="gva-search-box">
      <el-form :inline="true" :model="searchInfo">
        <el-form-item label="分组编码">
          <el-input v-model="searchInfo.groupCode" placeholder="搜索条件" clearable/>
        </el-form-item>
        <el-form-item label="分组名称">
          <el-input v-model="searchInfo.groupName" placeholder="搜索条件" clearable/>
        </el-form-item>
		<el-form-item label="字典标识">
		  <el-input v-model="searchInfo.key" placeholder="搜索条件" clearable/>
		</el-form-item>
		<el-form-item label="备注">
		  <el-input v-model="searchInfo.remark" placeholder="搜索条件" clearable/>
		</el-form-item>
        <el-form-item label="启用状态" prop="state">
          <el-select v-model="searchInfo.state" placeholder="请选择" clearable>
			<el-option
			  v-for="item in stateOptions"
			  :key="item.value"
			  :label="`${item.label}`"
			  :value="item.value"
			/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            icon="search"
            @click="onSubmit"
          >查询</el-button>
          <el-button
            size="small"
            icon="refresh"
            @click="onReset"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
      <div class="gva-btn-list">
        <el-button
          size="small"
          type="primary"
          icon="plus"
          @click="openDialog"
        >新增</el-button>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        tooltip-effect="dark"
        row-key="id"
      >
        <el-table-column align="left" label="分组编码" prop="groupCode" width="140" />
        <el-table-column align="left" label="分组名称" prop="groupName" width="140" />
		<el-table-column align="left" label="字典标识" prop="key" width="160" />
		<el-table-column align="left" label="字典值" prop="value" width="120" />
		<el-table-column align="left" label="字典名称" prop="name" width="140" />
		<el-table-column align="left" label="数据类型" prop="dataType" width="80" />
		<el-table-column align="left" label="备注" prop="remark" width="200" />
        <el-table-column align="left" label="状态" prop="state" width="80">
		  <template #default="scope">
		    <div>
		      {{ scope.row.state == 1 ? "启用" : "停用" }}
		    </div>
		  </template>
        </el-table-column>
		<el-table-column align="left" label="排序" prop="sort" width="60" />
        <el-table-column align="left" label="按钮组">
          <template #default="scope">
            <el-button
              size="small"
              icon="edit"
              type="text"
              @click="updateDictionaryFunc(scope.row,'edit')"
            >编辑</el-button>
			<el-button
			  size="small"
			  icon="edit"
			  type="text"
			  @click="updateDictionaryFunc(scope.row,'copy')"
			>复制</el-button>
            <el-popover
              v-model:visible="scope.row.visible"
              placement="top"
              width="160"
            >
              <p>确定要删除吗？</p>
              <div style="text-align: right; margin-top: 8px">
                <el-button
                  size="small"
                  type="text"
                  @click="scope.row.visible = false"
                >取消</el-button>
                <el-button
                  type="primary"
                  size="small"
                  @click="deleteSysDictionaryFunc(scope.row)"
                >确定</el-button>
              </div>
              <template #reference>
                <el-button
                  type="text"
                  icon="delete"
                  size="small"
                  style="margin-left: 10px"
                  @click="scope.row.visible = true"
                >删除</el-button>
              </template>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>

      <div class="gva-pagination">
        <el-pagination
          :current-page="pageNumber"
          :page-size="pageSize"
          :page-sizes="[10, 30, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <el-dialog
      v-model="dialogFormVisible"
      :before-close="closeDialog"
      :title="dialogTitle"
    >
      <el-form
        ref="dialogForm"
        :model="formData"
        :rules="rules"
        label-width="110px"
      >
        <el-form-item label="分组编码" prop="groupCode">
          <el-input v-model="formData.groupCode" placeholder="请输入分组编码" clearable :style="{ width: '100%' }" />
        </el-form-item>
        <el-form-item label="分组名称" prop="groupName">
          <el-input v-model="formData.groupName" placeholder="请输入分组名称" clearable :style="{ width: '100%' }" />
        </el-form-item>
		<el-form-item label="字典标识" prop="key">
		  <el-input v-model="formData.key" placeholder="请输入字典标识" clearable :style="{ width: '100%' }" />
		</el-form-item>
		<el-form-item label="字典值" prop="value">
		  <el-input v-model="formData.value" placeholder="请输入字典值" clearable :style="{ width: '100%' }" />
		</el-form-item>
		<el-form-item label="字典名称" prop="name">
		  <el-input v-model="formData.name" placeholder="请输入字典名称" clearable :style="{ width: '100%' }" />
		</el-form-item>
		<el-form-item label="数据类型" prop="dataType">
		  <!-- <el-input v-model="formData.dataType" placeholder="请输入数据类型" clearable :style="{ width: '100%' }" /> -->
		  <el-select v-model="formData.dataType" style="width: 100%;">
		    <el-option value="int" label="int" />
		    <el-option value="string" label="string" />
		  </el-select>
		</el-form-item>
		<el-form-item label="备注" prop="remark">
		  <el-input v-model="formData.remark" placeholder="请输入备注" clearable :style="{ width: '100%' }" />
		</el-form-item>
        <el-form-item label="启用状态" prop="state">
          <!-- <el-switch
            v-model="formData.state"
            active-text="启用"
			:active-value=1
            inactive-text="停用"
			:inactive-value=-1
          /> -->
		  <el-select v-model="formData.state" placeholder="请选择" style="width: 140px;">
			<el-option
			  v-for="item in stateOptions"
			  :key="item.value"
			  :label="`${item.label}`"
			  :value="item.value"
			/>
		  </el-select>
        </el-form-item>
		<el-form-item label="排序" prop="sort">
		  <el-input v-model.number="formData.sort" placeholder="请输入排序" clearable :style="{ width: '100%' }" />
		</el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button size="small" @click="closeDialog">取 消</el-button>
          <el-button
            size="small"
            type="primary"
            @click="enterDialog"
          >确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'SysDictionary',
}
</script>

<script setup>
import {
  saveDictionary,
  deleteDictionary,
  getDictionaryList,
} from '@/api/dictionary' //  此处请自行替换地址
import { getDict } from '@/utils/dictionary'
import warningBar from '@/components/warningBar/warningBar.vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

const formData = ref({
  value: '',
  name: '',
  dataType: 'int',
  state: 1,
  remark: '',
  sort: 1
})
const rules = ref({
  groupCode: [
    {
      required: true,
      message: '请输入分组编码',
      trigger: 'blur',
    },
  ],
  groupName: [
    {
      required: true,
      message: '请输入分组名称',
      trigger: 'blur',
    },
  ],
  value: [
    {
      required: true,
      message: '请输入字典值',
      trigger: 'blur',
    },
  ],
  name: [
    {
      required: true,
      message: '请输入字典名称',
      trigger: 'blur',
    },
  ],
})

const pageNumber = ref(1)
const total = ref(0)
const pageSize = ref(10)
const tableData = ref([])
const searchInfo = ref({})

const stateOptions = ref([])

const onReset = () => {
  searchInfo.value = {}
}

// 条件搜索前端看此方法
const onSubmit = () => {
  pageNumber.value = 1
  pageSize.value = 10
  if (searchInfo.value.status === '') {
    searchInfo.value.status = null
  }
  getTableData()
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

const handleCurrentChange = (val) => {
  pageNumber.value = val
  getTableData()
}

// 查询
const getTableData = async() => {
  const table = await getDictionaryList({
    pageNumber: pageNumber.value,
    pageSize: pageSize.value,
    ...searchInfo.value,
  })
  if (table.code === 0) {
    tableData.value = table.data.list
    total.value = table.data.total
  }
}

const initPage = async() => {
	{
		  var op = await getDict("initiate_mode")
		  if(op){
			  stateOptions.value = op
		  }
	}
	getTableData()
}

initPage()

const dialogFormVisible = ref(false)
const dialogTitle = ref('新增字典')
const updateDictionaryFunc = async(row,action) => {
	formData.value = row
	if("edit" === action){
		dialogTitle.value = "编辑字典"
	} else if("copy" === action){
		dialogTitle.value = "复制字典"
		delete formData.value["id"]
	}
    dialogFormVisible.value = true
}
const closeDialog = () => {
  dialogFormVisible.value = false
  formData.value = {
    value: '',
    name: '',
	dataType: 'int',
    state: 1,
    remark: '',
	sort: 1
  }
}
const deleteSysDictionaryFunc = async(row) => {
  row.visible = false
  const res = await deleteDictionary({ id: row.id })
  if (res.code === 0) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    if (tableData.value.length === 1 && pageNumber.value > 1) {
      pageNumber.value--
    }
    getTableData()
  }
}

const dialogForm = ref(null)
const enterDialog = async() => {
  dialogForm.value.validate(async(valid) => {
    if (!valid) return
    let res = await saveDictionary(formData.value)
    if (res.code === 0) {
      closeDialog()
      getTableData()
    }
  })
}
const openDialog = () => {
  dialogFormVisible.value = true
}
</script>

<style></style>
